package com.aysafety.eventmodel.service.tree.impl;

import com.aysafety.eventmodel.common.DateUtils;
import com.aysafety.eventmodel.dao.TreeDao;
import com.aysafety.eventmodel.service.tree.TreeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TreeServiceImpl implements TreeService {


    @Autowired TreeDao treeDao;

    private final String serviceName = "treeService:";

    @Override public long countTreeData(String entId, String type) throws Throwable {
        List<Map<String, Object>> conditions = new ArrayList<Map<String, Object>>();
        Map<String, Object> parram = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(entId)) {
            parram.put("entId", "\'" +entId+ "\'");
        }
        if (StringUtils.isNotBlank(type)) {
            parram.put("type", "\'" +type+ "\'");
        }
        long count = treeDao.countTreeData(parram);
        return count;
    }

    @Override public List<Map<String, Object>> queryTreeData(String entId, String type, int page, int size, String startRow)
        throws Throwable {
        List<Map<String, Object>> conditions = new ArrayList<Map<String, Object>>();
        Map<String, Object> parram = new HashMap<String, Object>();
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        if (StringUtils.isNotBlank(entId)) {
            parram.put("entId", "\'" +entId+ "\'");
        }
        if (StringUtils.isNotBlank(type)) {
            parram.put("type", "\'" +type+ "\'");
        }
        List<Map<String, Object>> datas = treeDao.queryTreeData(parram);
        for (Map<String, Object> data : datas) {
            long endTime = System.currentTimeMillis();;
            long startTime = (long) data.get("datetime");
            long temp = endTime / 1000 - startTime / 1000;
            if (temp > 0 && temp <= 21600) {
                data.put("status", 1);
            } else {
                data.put("status", 0);
            }
            data.put("rowkey",data.get("PK"));
            data.remove("PK");
            data.put("lastDatetime", DateUtils.getTime(DateUtils.fomatDateTime(startTime)));
        }
        return datas;
    }
}
